﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class UsersProjects : PageBaseClass
{
    UserInProjectFactory uipFactory;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DepartmentFactory dFactory = new DepartmentFactory();
            List<Department> deptList = dFactory.GetAllBy(Department.DepartmentFields.Office_Code, _author.office_Code);

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = Department.DepartmentFields.Department_Name.ToString();
            ddlDept.DataValueField = Department.DepartmentFields.Department_Code.ToString();
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }

            ProjectFactory pFactory = new ProjectFactory();
            List<Project> lstProj = pFactory.GetAllBy(Project.ProjectFields.Office_Code, _author.office_Code);

            ddlProjects.DataSource = lstProj;
            ddlProjects.DataTextField = Project.ProjectFields.Project_Title.ToString();
            ddlProjects.DataValueField = Project.ProjectFields.Project_Code.ToString();
            ddlProjects.DataBind();

            if (lstProj.Count > 0)
            {
                ddlProjects.SelectedIndex = 0;
                BindTheProjectUsers(int.Parse(ddlProjects.SelectedValue));
            }
            else
            {
                BindTheProjectUsers(0);
            }

        }
    }

    private void BindTheUsers(int deptCode)
    {
        if (deptCode >= 0)
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAllBy(UserInfo.UserInfoFields.Department_Code, deptCode);
        }
        else
        {
            this.ddlUsers.DataSource = new UserInfoFactory().GetAll();
        }

        this.ddlUsers.DataTextField = UserInfo.UserInfoFields.LastName.ToString();// +", " + UserInfo.UserInfoFields.LastName.ToString();
        this.ddlUsers.DataTextFormatString = "Ms {0}";
        this.ddlUsers.DataValueField = UserInfo.UserInfoFields.User_Code.ToString();
        this.ddlUsers.DataBind();
    }

    private void BindTheProjectUsers(int projectCode)
    {
        object dataObject = null;
        new SqlFactory().ExecuteQuery("MAN_Select_UsersInAProject " + projectCode, out dataObject, SqlFactory.DataObjectType.DataSet);
        //if (projectCode >= 0)
        //{
        //    this.gvUserProjects.DataSource = new UserInProjectFactory().GetAllBy(UserInProject.UserInProjectFields.Project_Code, projectCode);
        //}
        //else
        //{
        //    this.gvUserProjects.DataSource = new UserInProjectFactory().GetAll();
        //}
        if (dataObject != null)
        {
            this.gvUserProjects.DataSource = dataObject;
            this.gvUserProjects.DataBind();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserInProject uiProj = new UserInProject();
        uiProj.Project_Code = int.Parse(ddlProjects.SelectedValue);
        uiProj.User_Code = int.Parse(ddlUsers.SelectedValue);

        uipFactory = new UserInProjectFactory();
        if (uipFactory.Insert(uiProj))
        {
            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }

    protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProjects.SelectedIndex >= 0)
        {
            BindTheProjectUsers(int.Parse(ddlProjects.SelectedValue));
        }
        else
        {
            BindTheProjectUsers(0);
        }
    }
}
